package com.example.emos.api.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.emos.api.common.util.PageUtils;
import com.example.emos.api.exception.EmosException;
import com.example.emos.api.service.ApprovalService;
import com.example.emos.api.service.WorkflowService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 审批接口实现类
 *
 * @author: LC
 * @date 2022/4/7 15:35
 * @ClassName: ApprovalServiceImpl
 */
@Service
@Slf4j
public class ApprovalServiceImpl implements ApprovalService {

    @Value("${workflow.url}")
    private String workflow;

    @Value("${emos.code}")
    private String code;

    @Value("${emos.tcode}")
    private String tcode;

    @Autowired
    private WorkflowService workflowService;

    @Override
    public PageUtils searchTaskByPage(HashMap param) {
        //原代码
        param.put("code", code);
        param.put("tcode", tcode);
        String url = workflow + "/workflow/searchTaskByPage";
        HttpResponse response = HttpRequest.post(url).header("Content-Type", "application/json")
                .body(JSONUtil.toJsonStr(param)).execute();
        if (response.getStatus() == 200) {
            JSONObject json = JSONUtil.parseObj(response.body());
            JSONObject page = json.getJSONObject("page");
            ArrayList list = page.get("list", ArrayList.class);
            Long totalCount = page.getLong("totalCount");
            Integer pageIndex = page.getInt("pageIndex");
            Integer pageSize = page.getInt("pageSize");
            PageUtils pageUtils = new PageUtils(list, totalCount, pageIndex, pageSize);
            return pageUtils;
        } else {
            log.error(response.body());
            throw new EmosException("获取工作流数据异常");
        }
        //更改后代码
//        int page = MapUtil.getInt(param, "page");
//        int length = MapUtil.getInt(param, "length");
//        int start = (page - 1) * length;
//        param.remove("page");
//        param.put("start", start);
//        //需要强转下param role
//        param.get("role");
//        HashMap map = workflowService.searchTaskByPage(param);
//        ArrayList list = (ArrayList) map.get("list");
//        Long totalCount = MapUtil.getLong(map, "totalCount");
//        Integer pageIndex = MapUtil.getInt(map, "pageIndex");
//        Integer pageSize = MapUtil.getInt(map, "pageSize");
//        PageUtils pageUtils = new PageUtils(list, totalCount, pageIndex, pageSize);
//        return pageUtils;
    }

    @Override
    public HashMap searchApprovalContent(HashMap param) {
        param.put("code", code);
        param.put("tcode", tcode);
        String url = workflow + "/workflow/searchApprovalContent";
        HttpResponse response = HttpRequest.post(url).header("Content-Type", "application/json")
                .body(JSONUtil.toJsonStr(param)).execute();
        if (response.getStatus() == 200) {
            JSONObject jsonObject = JSONUtil.parseObj(response.body());
            HashMap content = jsonObject.get("content", HashMap.class);
            return content;
        } else {
            log.error(response.body());
            throw new EmosException("获取工作流数据异常");
        }
    }

    @Override
    public void approvalTask(HashMap param) {
        param.put("code", code);
        param.put("tcode", tcode);
        String url = workflow + "/workflow/approvalTask";
        HttpResponse response = HttpRequest.post(url).header("Cpntent-Type", "application/json")
                .body(JSONUtil.toJsonStr(param)).execute();
        if (response.getStatus() != 200) {
            log.error(response.body());
            throw new EmosException("调用工作流审批异常");
        }
    }

    @Override
    public void archiveTask(HashMap param) {
        param.put("code", code);
        param.put("tcode", tcode);
        String url = workflow + "/workflow/archiveTask";
        HttpResponse response = HttpRequest.post(url).header("Content-Type", "application/json")
                .body(JSONUtil.toJsonStr(param)).execute();
        if (response.getStatus() != 200) {
            log.error(response.body());
            throw new EmosException("调用工作流审批异样");
        }
    }
}
